<?php
namespace app\platform\controller\admin;
use app\BaseController;
use app\beautiful\model\finance\WithdrawModel;
use app\stores\model\withdraw\MemberWithdrawModel;

class  Export extends BaseController{
    /**
     * 商家提现已完成导出
     */
    public function shopWithdrawExport()

    {
        $name = $this->request->param('name');
        $start_time = $this->request->param('start_time');
        $end_time = $this->request->param('end_time');
        $status = [-1,1];

        if($start_time && $end_time){
            $where['start_time'] = $start_time;
            $where['end_time'] = $end_time;
        }
        $order=['add_time'=>'desc'];
        $where['status'] = $status;
        $where['name'] = $name;
        $return = WithdrawModel::list($where,$order,0,0);
        $list = $return['list'];
        //导出数据
        $titleArray = array();
        $titleArray[] = '提现商家';
        $titleArray[] = '提现金额';
        $titleArray[] = '手续费';
        $titleArray[] = '到账金额';
        $titleArray[] = '提交时间';
        $titleArray[] = '处理时间';
        $titleArray[] = '结算账户户名';
        $titleArray[] = '结算账户账号';
        $titleArray[] = '状态';
        $titleArray[] = '备注';
        $titleArray[] = '凭证';
        $filename = '商家提现数据' . date('YmdHis') . '.csv';

        ob_start();
        header("Expires:-1");
        header("Cache-Control:no_cache");
        header("Pragma:no-cache");
        header("Content-Type: text/csv");
        header("Content-Disposition:filename=" . $filename);
        $fp = fopen('php://output', 'w');

        //转码 防止乱码(比如微信昵称(乱七八糟的))
        fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));
        fputcsv($fp, $titleArray);

        $index = 0;
        foreach ($list as $k=>$row){
            $data = [];
            $data[] = $row['shop_name'];
            $data[] = $row['money'];
            $data[] = $row['fee'];
            $data[] = $row['received_money'];
            $data[] = $row['add_time']."\t";
            $data[] = $row['pay_time']."\t";
            $data[] = $row['account_name'];
            $data[] = $row['account_number']."\t";
            $data[] = $row['status']==1?'成功':'失败';
            $data[] = $row['remarks'];
            $data[] = $row['pay_voucher'];
            $index++;
            fputcsv($fp, $data);

            if ($index == 1000) {
                $index = 0;
                ob_flush();
                flush();
            }
        }
        ob_flush();
        flush();
        ob_end_clean();
        exit();
    }
    /**
     * 用户提现已完成导出
     */
    public function memberWithdrawExport()

    {
        $name = $this->request->param('name');
        $status = 1;
        if($status==1){
            $status = [-1,1];
        }else{
            $status = [0,2];
        }
        $order=$status==1?['pay_time'=>'desc']:['add_time'=>'desc'];
        $where['status'] = $status;
        $where['name'] = $name;
        $return = MemberWithdrawModel::list($where,$order,0,0);
        $list = $return['list'];
        //导出数据
        $titleArray = array();
        $titleArray[] = '提现用户';
        $titleArray[] = '提现金额';
        $titleArray[] = '手续费';
        $titleArray[] = '到账金额';
        $titleArray[] = '提交时间';
        $titleArray[] = '处理时间';
        $titleArray[] = '结算账户户名';
        $titleArray[] = '结算账户账号';
        $titleArray[] = '状态';
        $titleArray[] = '备注';
        $titleArray[] = '凭证';
        $filename = '用户提现数据' . date('YmdHis') . '.csv';

        ob_start();
        header("Expires:-1");
        header("Cache-Control:no_cache");
        header("Pragma:no-cache");
        header("Content-Type: text/csv");
        header("Content-Disposition:filename=" . $filename);
        $fp = fopen('php://output', 'w');

        //转码 防止乱码(比如微信昵称(乱七八糟的))
        fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));
        fputcsv($fp, $titleArray);

        $index = 0;
        foreach ($list as $k=>$row){
            $data = [];
            $data[] = $row['nick_name'];
            $data[] = $row['money'];
            $data[] = $row['fee'];
            $data[] = $row['received_money'];
            $data[] = $row['add_time']."\t";
            $data[] = $row['pay_time']."\t";
            $data[] = $row['account_name'];
            $data[] = $row['account_number']."\t";
            $data[] = $row['status']==1?'成功':'失败';
            $data[] = $row['remarks'];
            $data[] = $row['pay_voucher'];
            $index++;
            fputcsv($fp, $data);

            if ($index == 1000) {
                $index = 0;
                ob_flush();
                flush();
            }
        }
        ob_flush();
        flush();
        ob_end_clean();
        exit();
    }
} 